package easymall.controller;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import easymall.po.Order;
import easymall.po.OrderItem;
import easymall.po.Products;
import easymall.po.User;
import easymall.pojo.MyCart;
import easymall.pojo.OrderInfo;
import easymall.service.CartService;
import easymall.service.OrderService;
import easymall.service.ProductsService;

@Controller("orderController")
@RequestMapping("/order")
public class OrderController extends BaseController{
	
	@Autowired
	private CartService cartService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private ProductsService productsService;
	
	@RequestMapping("/order_add")
	public String order_add(String cartIds, Model model) {
		//讲购物车中所有选中商品的cartID组合起来的字符串拆分为数组
		if(cartIds == "" || cartIds==null) return "redirect:/cart/showCart";
		String[] arrCartIds = cartIds.split(",");
		
		List<MyCart> carts = new ArrayList<MyCart>();
		//遍历数组 根据cartID编号查找购物车 添加到carts中
		for(String cid : arrCartIds) {
			Integer cartID = Integer.parseInt(cid);
			MyCart cart = cartService.findByCartID(cartID);
			carts.add(cart);
		}
		model.addAttribute("carts", carts);
		model.addAttribute("cartIds", cartIds);
		return("order_add");
	}
	
	@RequestMapping("/addOrder")
	public String addOrder(String receiverinfo, String cartIds, HttpSession session) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = df.format(new Date());
		Timestamp timestamp = Timestamp.valueOf(time);
		User user = (User)session.getAttribute("user");
		String orderId = UUID.randomUUID().toString();
		Order myOrder = new Order(orderId,null,receiverinfo,"未支付",timestamp,user.getId());
		orderService.addOrder(cartIds, myOrder);
		return "forward:/order/showOrder";
	}
	
	@RequestMapping("/showOrder")
	public String showOrder(HttpSession session, Model model) {
		User user = (User)session.getAttribute("user");
		List<OrderInfo> orderInfoList = findOrderInfoByUserId(user.getId());
		model.addAttribute("orderInfos", orderInfoList);
		return "order_list";
	}
	
	//根据用户id获取该用户所有的订单信息，包括每个订单的商品详情
	private List<OrderInfo> findOrderInfoByUserId(Integer user_id){
		List<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
		List<Order> orderList = orderService.findOrderByUserId(user_id);
		for(Order order : orderList) {
			List<OrderItem> orderItems = orderService.getOrderItemsByOrderId(order.getId());
			Map<Products, Integer> map = new HashMap<Products, Integer>();
			for(OrderItem orderItem : orderItems) {
				Products product = productsService.oneProduct(orderItem.getProduct_id());
				map.put(product, orderItem.getBuynum());
			}
			OrderInfo orderInfo = new OrderInfo();
			orderInfo.setOrder(order);
			orderInfo.setMap(map);
			orderInfoList.add(orderInfo);
		}
		return orderInfoList;
	}
	
	@RequestMapping("/delOrder")
	public String delOrder(String id, Model model) {
		orderService.delOrder(id);
		return "redirect:/order/showOrder";
	}
	
	@RequestMapping("/payOrder")
	public String payOrder(String id, Model model) {
		orderService.payOrder(id);
		return "redirect:/order/showOrder";
	}
	//设置订单为确认收货
	@RequestMapping("/confirmOrder")
	public String confirmOrder(String id, Model model) {
		orderService.confirmOrder(id);
		return "redirect:/order/showOrder";
	}

}
